import {cloneDeep, isFunction} from "lodash"

const lifecycleMethods=['onLoad','onShow','onReady','onHide','onUnload','onPageScroll'];

class Page{
    constructor(pageModule,extraOption){
        this.pageModule=pageModule;
        this.extraOption=extraOption;
        this.id=extraOption.id;
        this.data=cloneDeep(pageModule.moduleInfo.data);
        this.lifecycleMethods();
        this.initMethods();
        this.onLoad(this.extraOption.query || {});
        this.onShow();
    }

    lifecycleMethods(){
        lifecycleMethods.forEach(method=>{
            if(!isFunction(this.pageModule.moduleInfo[method])){
                return false;
            }
            this[method]=this.pageModule.moduleInfo[method].bind(this);
        })
    }
    initMethods(){
        const moduleInfo=this.pageModule.moduleInfo;
        for(let attr in moduleInfo){
            if(!isFunction(moduleInfo[attr]) && !lifecycleMethods.includes(attr)){
                this[attr]=moduleInfo[attr].bind(this);
            }
        }
    }

}

export default new Page();
